package com.huawei.secure.android.common.encrypt.hash;

import android.text.TextUtils;
import android.util.Log;
import com.huawei.secure.android.common.encrypt.utils.EncryptUtil;
import com.huawei.secure.android.common.encrypt.utils.a;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public abstract class PBKDF2 {
    private static final String TAG = "PBKDF2";
    private static final String i = "";
    private static final String u = "PBKDF2WithHmacSHA1";
    private static final int v = 8;
    private static final int w = 32;
    private static final int x = 10000;
    private static final int y = 1000;

    public static String pbkdf2Encrypt(String str) {
        return pbkdf2Encrypt(str, 10000);
    }

    public static String pbkdf2Encrypt(String str, int i) {
        return pbkdf2Encrypt(str, EncryptUtil.generateSecureRandom(8), i, 32);
    }

    public static String pbkdf2Encrypt(String str, byte[] bArr, int i, int i2) {
        if (TextUtils.isEmpty(str)) {
            Log.e(TAG, "pwd is null.");
            return "";
        } else if (i < 1000) {
            Log.e(TAG, "iterations times is not enough.");
            return "";
        } else if (bArr == null || bArr.length < 8) {
            Log.e(TAG, "salt parameter is null or length is not enough");
            return "";
        } else if (i2 < 32) {
            Log.e(TAG, "cipherLen length is not enough");
            return "";
        } else {
            return a.f(bArr) + a.f(pbkdf2(str.toCharArray(), bArr, i, i2 * 8));
        }
    }

    public static boolean validatePassword(String str, String str2) {
        return validatePassword(str, str2, 10000);
    }

    public static boolean validatePassword(String str, String str2, int i) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || str2.length() < 16) {
            return false;
        }
        return b(pbkdf2(str.toCharArray(), a.l(str2.substring(0, 16)), i, 256), a.l(str2.substring(16)));
    }

    public static byte[] pbkdf2(char[] cArr, byte[] bArr, int i, int i2) {
        GeneralSecurityException e;
        try {
            return SecretKeyFactory.getInstance(u).generateSecret(new PBEKeySpec(cArr, bArr, i, i2)).getEncoded();
        } catch (NoSuchAlgorithmException e2) {
            e = e2;
            Log.e(TAG, e.getMessage());
            return new byte[0];
        } catch (InvalidKeySpecException e3) {
            e = e3;
            Log.e(TAG, e.getMessage());
            return new byte[0];
        }
    }

    private static boolean b(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr2 == null) {
            return false;
        }
        int length = bArr.length ^ bArr2.length;
        int i = 0;
        while (i < bArr.length && i < bArr2.length) {
            length |= bArr[i] ^ bArr2[i];
            i++;
        }
        if (length == 0) {
            return true;
        }
        return false;
    }
}
